
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.core.PopupView
 *	
 *	@version 1.00 | Jan 20, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.core 
{
	import uk.co.lmfm.guppy.view.components.events.PopUpViewEvent;

	import com.bit101.components.Label;
	import com.bit101.components.PushButton;

	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * PopupView
	 */
	public class PopupView extends AbstractView implements IViewComponent 
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		private static const MIN_WIDTH : int = 240;
		private static const MIN_HEIGHT : int = 40;

		//	----------------------------------------------------------------
		//	PROTECTED MEMBERS
		//	----------------------------------------------------------------

		protected var _title : Label = new Label();
		protected var _okButton : PushButton = new PushButton();		protected var _cancelButton : PushButton = new PushButton();

		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _content : Sprite;

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function PopupView(__title : String, __content : Sprite = null, __confirmLabel : String = "OK", __cancelLabel : String = null, __autoShow : Boolean = true)
		{
			super();
			
			_title.text = __title;
			_content = __content;
			
			addChild(_title);
			addChild(__content);
			
			if(__confirmLabel)
			{
				_okButton.label = __confirmLabel;
				addChild(_okButton);
			}
			
			if(__cancelLabel)
			{
				_cancelButton.label = __cancelLabel;
				addChild(_cancelButton);
			}
			
			_okButton.addEventListener(MouseEvent.CLICK, onButtonPressed);			_cancelButton.addEventListener(MouseEvent.CLICK, onButtonPressed);
			
			if(__autoShow) show();
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			if(!stage) return;
			
			super.resize();
			
			var w : int = Math.max(MIN_WIDTH, _content.width + 40);
			var h : int = Math.max(MIN_HEIGHT, _title.y + _title.height + 5 + 20 + _content.height + 20 + _okButton.height + 20);
			
			var w2 : int = w >> 1;			var h2 : int = h >> 1; 
			h2;
			
			var sw : int = stage.stageWidth >> 1;
			var sh : int = stage.stageHeight >> 1;
			
			_title.x = w2 - (_title.width >> 1);
			_title.y = 5;
			
			_content.x = w2 - (_content.width >> 1);
			_content.y = _title.y + _title.height + 20;
			
			_okButton.x = contains(_cancelButton) ? w2 + 10 : w2 - (_okButton.width >> 1);
			_cancelButton.x = contains(_cancelButton) ? w2 - _cancelButton.width - 10 : 0;
			_okButton.y = _cancelButton.y = h - _okButton.height - 10;
			
			x = sw - (w * 0.5);
			y = sh - (h * 0.5);
			
			graphics.clear();
			
			graphics.beginFill(0xCCCCCC, 0.6);
			graphics.drawRect(-x, -y, stage.stageWidth, stage.stageHeight);
			graphics.endFill();
			
			graphics.lineStyle(6, 0xCCCCCC, 0.6);
			graphics.drawRect(0, 0, w, h);
			
			graphics.lineStyle(0, 0xCCCCCC);
			graphics.beginFill(0xf2f2f2);
			graphics.drawRect(0, 0, w, h);
			
			graphics.moveTo(0, _title.y + _title.height + 5);
			graphics.lineTo(w, _title.y + _title.height + 5);
			
			graphics.moveTo(0, _okButton.y - 10);
			graphics.lineTo(w, _okButton.y - 10);
			graphics.endFill();
		}

		override public function show() : void
		{
			resize();
			super.show();
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onButtonPressed(event : MouseEvent) : void
		{
			switch(event.currentTarget)
			{
				case _okButton : 
				
					dispatchEvent(new PopUpViewEvent(PopUpViewEvent.CONFIRM));
					
					break;
									case _cancelButton : 
				
					dispatchEvent(new PopUpViewEvent(PopUpViewEvent.CANCEL));
					
					break;
			}
			
			hide();
		}
	}
}
